﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Networking
{
    /// <summary>
    /// Packet of type SetPixelFormatPacket, it contains all the pixel format parameters
    /// </summary>
    public class SetPixelFormatPacket : Packet
    {

        /// <summary>
        /// The pixel format parameters
        /// </summary>
        public PixelFormat PixelFormat { get; private set; }

        /// <summary>
        /// Creates a new instance of SetPixelFormatPacket specifying all the pixel parameters
        /// </summary>
        /// <param name="PixelFormat">The pixel format parameters</param>
        public SetPixelFormatPacket(PixelFormat PixelFormat) 
            : base(PacketType.SetPixelFormat)
        {
            this.PixelFormat = PixelFormat;
        }

        /// <summary>
        /// Creates a new instance of SetPixelFormatPacket extracting all the parameters from the byte buffer
        /// </summary>
        /// <param name="buffer">The byte buffer containing the packet data</param>
        public SetPixelFormatPacket(byte[] buffer)
            : base(PacketType.SetPixelFormat, buffer)
        {
            //
        }
        
        /// <summary>
        /// Serializes the packet in order to send it
        /// </summary>
        /// <returns>The bytes of the packet</returns>
        public override byte[] Serialize()
        {

            MemoryStream memory = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(memory);

            writer.Write((byte)this.Type);

            // Padding
            writer.Write(new byte[] { 0, 0, 0 });

            writer.Write(this.PixelFormat.BitsPerPixel);
            writer.Write(this.PixelFormat.Depth);
            writer.Write((byte)(this.PixelFormat.IsBigEndian ? 0x1 : 0x0));
            writer.Write((byte)(this.PixelFormat.IsTrueColor ? 0x1 : 0x0));
            writer.Write(this.PixelFormat.RedMax);
            writer.Write(this.PixelFormat.GreenMax);
            writer.Write(this.PixelFormat.BlueMax);
            writer.Write(this.PixelFormat.RedShift);
            writer.Write(this.PixelFormat.GreenShift);
            writer.Write(this.PixelFormat.BlueShift);

            writer.Close();

            return memory.ToArray();
            
        }


        /// <summary>
        /// Loads a packet from a byte buffer
        /// </summary>
        /// <param name="buffer">The data buffer</param>
        public override void Load(byte[] buffer)
        {
            if (buffer.Length != 16) throw new BufferSizeMismatchException(16);

            MemoryStream memory = new MemoryStream(buffer);

            BinaryReader reader = new BinaryReader(memory);

            this.PixelFormat.BitsPerPixel = reader.ReadByte();
            this.PixelFormat.Depth = reader.ReadByte();
            this.PixelFormat.IsBigEndian = (reader.ReadByte() == 0x1) ? true : false;
            this.PixelFormat.IsTrueColor = (reader.ReadByte() == 0x1) ? true : false;
            this.PixelFormat.RedMax = reader.ReadInt16();
            this.PixelFormat.GreenMax = reader.ReadInt16();
            this.PixelFormat.BlueMax = reader.ReadInt16();
            this.PixelFormat.RedShift = reader.ReadByte();
            this.PixelFormat.GreenShift = reader.ReadByte();
            this.PixelFormat.BlueShift = reader.ReadByte();

            reader.Close();
        }
    }
}
